/*
 * Copyright 2009 Gary Brown
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *
 * Change History:
 * 15 Mar 2009 : Initial version created by gary
 */
package org.betonthemove.platform;

import org.betonthemove.platform.Market;

public class MarketGroup implements java.io.Serializable {

	private static final long serialVersionUID = 5710413636828515534L;

	public MarketGroup(String groupName, String path) {
		m_name = groupName;
		m_path = path;
	}
	
	/**
	 * This method returns the market group hierarchy. If
	 * no markets are provided, then no group will be returned.
	 * 
	 * @param markets The list of markets
	 * @return The top market group in the hierarchy, or null if
	 * 					no markets available
	 */
	public static MarketGroup build(java.util.List<Market> markets,
			java.util.Map<MarketGroup,java.util.List<Market>> groupMarkets,
			java.util.Map<MarketGroup,java.util.List<MarketGroup>> groups) {
		MarketGroup ret=null;
		
		for (int i=0; i < markets.size(); i++) {
			String menuPath=markets.get(i).getMenuPath();
			
			java.util.StringTokenizer st=new java.util.StringTokenizer(menuPath, "\\");
			
			MarketGroup cur=ret;
			String path="";
			
			while (st.hasMoreTokens()) {
				String token=st.nextToken();
				
				path += "\\"+token;
				
				if (cur == null) {
					ret = new MarketGroup(token, path);
					cur = ret;
					
				} else if (cur.getName().equals(token) == false) {
					java.util.List<MarketGroup> subGroups=
								groups.get(cur);
					
					if (subGroups == null) {
						subGroups = new java.util.Vector<MarketGroup>();
						groups.put(cur, subGroups);
					}
					
					MarketGroup sub=null;
					
					for (int j=0; sub == null &&
								j < subGroups.size(); j++) {
						if (subGroups.get(j).getName().equals(token)) {
							sub = subGroups.get(j);
						}
					}
					
					if (sub == null) {
						sub = new MarketGroup(token, path);
						subGroups.add(sub);
					}
					
					cur = sub;
				}
				
				cur.incrementCount();
			}
			
			java.util.List<Market> mlist=groupMarkets.get(cur);
			
			if (mlist == null) {
				mlist = new java.util.Vector<Market>();
				groupMarkets.put(cur, mlist);
			}
			
			mlist.add(markets.get(i));
		}
		
		return(ret);
	}
	
	public String getName() {
		return(m_name);
	}
	
	public void setName(String name) {
		m_name = name;
	}
	
	public String getPath() {
		return(m_path);
	}
	
	public void setPath(String path) {
		m_path = path;
	}
	
	public int getMarketCount() {
		return(m_marketCount);
	}
	
	public void incrementCount() {
		m_marketCount++;
	}
	
	public int hashCode() {
		int ret=0;
		
		if (m_path != null) {
			ret = m_path.hashCode();
		} else if (m_name != null) {
			ret = m_name.hashCode();
		}
		
		return(ret);
	}
	
	public boolean equals(Object obj) {
		boolean ret=false;
		
		if (obj instanceof MarketGroup) {
			MarketGroup mg=(MarketGroup)obj;
			
			if (mg.getPath() != null &&
					m_path != null) {
				ret = mg.getPath().equals(m_path);
			}
		}
		
		return(ret);
	}
	
	private String m_name=null;
	private String m_path=null;
	private int m_marketCount=0;
}
